home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8893 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Standard question - pointer initialization
  5. Date: Wed, 06 Mar 96 19:54:33 GMT
  6. Organization: none
  7. Message-ID: <826142073snz@genesis.demon.co.uk>
  8. References: <4hk9un$906@hammer.msfc.nasa.gov>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4hk9un$906@hammer.msfc.nasa.gov>
  15.            Brian.Day@msfc.nasa.gov "Brian Day" writes:
  16.  
  17. >Hi all,
  18. >
  19. >I don't have a copy of the ANSI standard handy, and was wondering
  20. >if y'all could answer a question for me.
  21. >
  22. >We were recently handed a coding standard which said:
  23. >
  24. >    "Always initialize pointers when they are declared" 
  25. >
  26. >Doesn't the standard say something about some pointers
  27. >being automatically initialized to NULL, or something like that?
  28.  
  29. Pointers are just like any other type of variable in this respect: static
  30. duration variables (i.e. defined as static or outside functions) are
  31. always initialised as program startup. They are initialised as if set to
  32. the value zero if there is no explicit initialisation in the source code.
  33. In the case of pointers this means they are initialised to null pointers
  34. of the respective type.
  35.  
  36. >Is this coding standard wise?
  37.  
  38. It is mostly if not entirely pointless. There is no point in initialising
  39. a variable if there is no sensible value to initialise it to. You might
  40. initialise pointers to the null pointer but the code might blow up anyway
  41. when encountering a null pointer. A much better approach is to use a
  42. compiler or failing that a lint utility which can detect and diagnose where
  43. code may be accessing uninitialised variables. This is a fairly routine
  44. dataflow analysis for a compiler although you may have to use a higher
  45. level of optimisation to get it.
  46.  
  47. It is more useful to initialise only those variables that need it,
  48. then when reading the code you can see where the initialisation is
  49. important i.e. you get more readable and maintainable code.
  50.  
  51. -- 
  52. -----------------------------------------
  53. Lawrence Kirby | fred@genesis.demon.co.uk
  54. Wilts, England | 70734.126@compuserve.com
  55. -----------------------------------------
  56.